﻿/*
 * Сделано в SharpDevelop.
 * Пользователь: maxxadm
 * Дата: 06.04.2012
 * Время: 19:22
 * 
 * Для изменения этого шаблона используйте Сервис | Настройка | Кодирование | Правка стандартных заголовков.
 */
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace mxplay
{
	/// <summary>
	/// Description of PlayListBindOptions.
	/// </summary>
	public class PlayListBindOptions
	{
		private PlayListBindOptions()
		{
		}
		
//		[CategoryLocalized("BassEngineCateg", typeof(lang))]
//		[DisplayNameLocalized("BassMono",ResourceType=typeof(lang))]
//		[DescriptionLocalized("BassMonoDescription",typeof(lang))]
//		[DefaultValue(false)]
//		[TypeConverter(typeof(BooleanLocalizedConvertor))]
		
//		public static StringCollection VisibleColumnNames
//		{
//			get
//			{
//				return mxplay.Properties.Settings.Default.PlaylistVisibleColumns;
//			}
//			set
//			{
//				mxplay.Properties.Settings.Default.PlaylistVisibleColumns=value;
//			}
//		}
		
		public static PropertyDescriptorCollection VisibleTagProperties
		{
			get
			{
				#if DEBUG
				Program.LogMessage("Getting props for visible columns...");
				#endif
				StringCollection visible_names=mxplay.Properties.Settings.Default.PlaylistVisibleColumns;
				Type playlistbindentry_type=typeof(PlayListBindEntry);
				PropertyDescriptorCollection props=TypeDescriptor.GetProperties(playlistbindentry_type);
				PropertyDescriptorCollection ret;
				List<PropertyDescriptor> temp_list=new List<PropertyDescriptor>();
				
				foreach(string one_col in visible_names)
				{
					PropertyDescriptor pd=props.Find(one_col,false);
					if(pd!=null) temp_list.Add(pd);
				}
				ret=new PropertyDescriptorCollection(temp_list.ToArray());
				return ret;
			}
			set
			{
				StringCollection visible_names=new StringCollection();
				foreach(PropertyDescriptor pd in value)
				{
					visible_names.Add(pd.Name);
				}
				mxplay.Properties.Settings.Default.PlaylistVisibleColumns=visible_names;
			}
		}
		
		
		public static PropertyDescriptorCollection GetAvailableTagProperties()
		{
			Type playlistbindentry_type=typeof(PlayListBindEntry);
			Attribute[] attributes=new Attribute[]{BrowsableAttribute.Yes};
			PropertyDescriptorCollection props=TypeDescriptor.GetProperties(playlistbindentry_type,attributes);
			return props;
		}
		
		public static PropertyDescriptorCollection AvailableTagProperties
		{
			get
			{
				StringCollection available_names=mxplay.Properties.Settings.Default.PlaylistAvailableColumns;
				Type playlistbindentry_type=typeof(PlayListBindEntry);
				PropertyDescriptorCollection props=TypeDescriptor.GetProperties(playlistbindentry_type);
				PropertyDescriptorCollection ret;
				List<PropertyDescriptor> temp_list=new List<PropertyDescriptor>();
				
				if((available_names==null)||(available_names.Count==0))
				{
					Attribute[] attributes=new Attribute[]{BrowsableAttribute.Yes};
					ret=TypeDescriptor.GetProperties(playlistbindentry_type,attributes);
				}
				else
				{
					foreach(string one_name in available_names)
					{
						PropertyDescriptor pd=props.Find(one_name,true);
						if (pd!=null) temp_list.Add(pd);
					}
					ret=new PropertyDescriptorCollection(temp_list.ToArray());
				}
				return ret;
			}
			set
			{
				StringCollection available_names=new StringCollection();
				foreach(PropertyDescriptor pd in value)
				{
					available_names.Add(pd.Name);
				}
				mxplay.Properties.Settings.Default.PlaylistAvailableColumns=available_names;
			}
		}
		
		public static void ShowColumnEditor(IWin32Window owner)
		{
			/*
			 * названия свойств херово отображаются
			 * надо как то перекрывать ToString()
			 * ибо checkedListBox не умеет брать атрибут DisplayName
			 * Теперь нормально отображаются - с помощью враппера
			 */
			ListEditorForm f=new ListEditorForm();
			
			List<PropertyDescriptorWrapper> available_props=new List<PlayListBindOptions.PropertyDescriptorWrapper>();
			foreach(PropertyDescriptor pd in AvailableTagProperties)
			{
				available_props.Add(new PropertyDescriptorWrapper(pd));
			}
			f.listEditorControl_internal.EnumAvailableItems=available_props;
			
			List<PropertyDescriptorWrapper> checked_props=new List<PlayListBindOptions.PropertyDescriptorWrapper>();
			foreach(PropertyDescriptor pd in VisibleTagProperties)
			{
				checked_props.Add(new PropertyDescriptorWrapper(pd));
			}
			f.listEditorControl_internal.EnumCheckedItems=checked_props;
			
			if((f.ShowDialog(owner)==DialogResult.OK)&&f.listEditorControl_internal.Changed)
			{
				List<PropertyDescriptor> available_list=new List<PropertyDescriptor>();
				foreach(object obj in f.listEditorControl_internal.EnumAvailableItems)
				{
					available_list.Add((obj as PropertyDescriptorWrapper).PropertyDescriptor);
				}
				AvailableTagProperties=new PropertyDescriptorCollection(available_list.ToArray());
				
				List<PropertyDescriptor> visible_list=new List<PropertyDescriptor>();
				foreach(object obj in f.listEditorControl_internal.EnumCheckedItems)
				{
					visible_list.Add((obj as PropertyDescriptorWrapper).PropertyDescriptor);
				}
				VisibleTagProperties=new PropertyDescriptorCollection(visible_list.ToArray());
			}
		}
		
		public class PropertyDescriptorWrapper : IEquatable<PropertyDescriptorWrapper>
		{
			public PropertyDescriptorWrapper(PropertyDescriptor pd)
			{
				PropertyDescriptor=pd;
			}
			
			public PropertyDescriptor PropertyDescriptor { get; set; }
			public override string ToString()
			{
				if(string.IsNullOrEmpty(PropertyDescriptor.DisplayName))
				{
					return PropertyDescriptor.Name;
				}
				else
				{
					return PropertyDescriptor.DisplayName;
				}
			}

			public bool Equals(PropertyDescriptorWrapper other)
			{
				if (other==null) return false;
				return PropertyDescriptor.Equals(other.PropertyDescriptor);
			}

			#region Equals and GetHashCode implementation
			public override bool Equals(object obj)
			{
				PlayListBindOptions.PropertyDescriptorWrapper other = obj as PlayListBindOptions.PropertyDescriptorWrapper;
				if (other == null)
					return false;
				return Equals(other);
			}
			
			public override int GetHashCode()
			{
				int hashCode = 0;
				unchecked
				{
					if (PropertyDescriptor != null)
						hashCode += 1000000007 * PropertyDescriptor.GetHashCode();
				}
				return hashCode;
			}
			
			public static bool operator ==(PlayListBindOptions.PropertyDescriptorWrapper lhs, PlayListBindOptions.PropertyDescriptorWrapper rhs)
			{
				if (ReferenceEquals(lhs, rhs))
					return true;
				if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null))
					return false;
				return lhs.Equals(rhs);
			}
			
			public static bool operator !=(PlayListBindOptions.PropertyDescriptorWrapper lhs, PlayListBindOptions.PropertyDescriptorWrapper rhs)
			{
				return !(lhs == rhs);
			}
			#endregion

		}
		
//		public static PropertyDescriptorCollection GetVisibleTagProperties()
//		{
//			StringCollection col_names=VisibleColumnNames;
//			Type playlistbindentry_type=typeof(PlayListBindEntry);
//			PropertyDescriptorCollection props=TypeDescriptor.GetProperties(playlistbindentry_type);
//			PropertyDescriptorCollection ret;
//			List<PropertyDescriptor> temp_list=new List<PropertyDescriptor>();
//
//			foreach(string one_col in col_names)
//			{
//				PropertyDescriptor pd=props.Find(one_col,false);
//				if(pd!=null) temp_list.Add(pd);
//			}
//			ret=new PropertyDescriptorCollection(temp_list.ToArray());
//			return ret;
//		}
//
//		public static PropertyDescriptorCollection GetVisibleTagProperties(Attribute[] attributes)
//		{
//			StringCollection col_names=VisibleColumnNames;
//			Type playlistbindentry_type=typeof(PlayListBindEntry);
//			PropertyDescriptorCollection props=TypeDescriptor.GetProperties(playlistbindentry_type,attributes);
//			PropertyDescriptorCollection ret;
//			List<PropertyDescriptor> temp_list=new List<PropertyDescriptor>();
//
//			foreach(string one_col in col_names)
//			{
//				PropertyDescriptor pd=props.Find(one_col,false);
//				if(pd!=null) temp_list.Add(pd);
//			}
//			ret=new PropertyDescriptorCollection(temp_list.ToArray());
//			return ret;
//		}
		
		
	}
}
